package com.yunxi.service.esb.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.openservices.ons.api.SendResult;
import com.yunxi.claim.pojo.*;
import com.yunxi.claim.service.*;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.bom.CatalogVO;
import com.yunxi.eai.pojo.bom.ModelVO;
import com.yunxi.eai.pojo.bom.PackageVO;
import com.yunxi.eai.pojo.cms.*;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.pojo.mes.VehicleMftInfoVO;
import com.yunxi.eai.pojo.mes.VehicleTrackVO;
import com.yunxi.eai.pojo.sap.ProductionPlanVO;
import com.yunxi.eai.pojo.ups.MaintainOrderVO;
import com.yunxi.eai.service.esb.OneToManyService;
import com.yunxi.hubToBaseData.pojo.VehicleConfigReqVO;
import com.yunxi.hubToBaseData.pojo.VehiclePackageReqVO;
import com.yunxi.hubToBaseData.service.BaseDataSaveService;
import com.yunxi.hubToBiz.pojo.HubProducePlanVO;
import com.yunxi.hubToBiz.pojo.HubProduceTracePlanVO;
import com.yunxi.hubToBiz.pojo.HubVehicleMftInfoVO;
import com.yunxi.hubToBiz.pojo.PackageConstraintVO;
import com.yunxi.hubToBiz.service.ConstraintConfigDubboService;
import com.yunxi.hubToBiz.service.HubProduceTraceService;
import com.yunxi.hubToBiz.service.ProducePlanBroadcastDubboService;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.parts.pojo.PrEpcCatalogCreateReqVO;
import com.yunxi.parts.pojo.PrEpcPartCreateReqVO;
import com.yunxi.parts.pojo.PrEpcPartReplacementCreateReqVO;
import com.yunxi.parts.service.EaiEpcCatalogDubboService;
import com.yunxi.parts.service.EaiEpcPartDubboService;
import com.yunxi.parts.service.EaiEpcPartReplacementDubboService;
import com.yunxi.rocketmq.producer.CmsBaseManHourStepProducer;
import com.yunxi.rocketmq.producer.CmsHourStepProducer;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.hubToBaseData.pojo.VehicleSpectrumReqVO;
import com.yunxi.tech.pojo.VehicleOfflineReqVO;
import com.yunxi.tech.service.IamVehicleSpectrumDubboService;
import com.yunxi.tech.service.SapVehicleOfflineDubboService;
import org.dozer.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/6/1.
 */
@Service
@Qualifier("oneToManyServiceImpl")
public class OneToManyServiceImpl implements OneToManyService {
    protected static final Logger LOGGER = LoggerFactory.getLogger(OneToManyServiceImpl.class);

    @Autowired
    BaseDataSaveService baseDataSaveServiceByBub;
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Autowired
    private ProducePlanBroadcastDubboService producePlanBroadcastDubboServiceImpl;
    @Autowired
    HubProduceTraceService hubProduceTraceServiceImpl;
    @Autowired
    CmLabourDirectoryDubboService cmLabourDirectoryDubboServiceImpl;
    @Autowired
    CmLabourProjectDubboService cmLabourProjectDubboServiceImpl;
    @Autowired
    CmLabourEntryDubboService cmLabourEntryDubboServiceImpl;
    @Autowired
    CmLabourStepDubboService cmLabourStepDubboServiceImpl;
    @Autowired
    CmLabourBasicStepDubboService cmLabourBasicStepDubboServiceImpl;
    @Autowired
    EaiEpcCatalogDubboService eaiEpcCatalogDubboServiceImpl;
    @Autowired
    EaiEpcPartDubboService eaiEpcPartDubboServiceImpl;
    @Autowired
    EaiEpcPartReplacementDubboService eaiEpcPartReplacementDubboServiceImpl;
    @Autowired
    CmsHourStepProducer cmsHourStepProducer;
    @Autowired
    CmsBaseManHourStepProducer cmsBaseManHourStepProducer;
    @Autowired
    IamVehicleSpectrumDubboService iamVehicleSpectrumDubboServiceImpl;
    @Autowired
    SapVehicleOfflineDubboService sapVehicleOfflineDubboServiceImpl;

    @Value("${APP.URL}")
    private String APP_URL;
    @Autowired
    private Mapper dozerMapper;
    @Autowired
    private ConstraintConfigDubboService constraintConfigDubboServiceImpl;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Autowired
    EaiCmRepairOrderDubboService cmRepairOrderDubboServiceImpl;
    @Override
    public ApiResult distributeData(EsbVO esbVO) {
        String serviceName = esbVO.getServiceName();
        // String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        if (serviceName.equals(Constant.BOM_MODEL)) {
            //型谱
            apiResult = saveVehicleSpectrum(esbVO);
        } else if (serviceName.equals(Constant.BOM_CATALOG)) {
            //工程配置
            apiResult = saveVehicleConfig(esbVO);
        } else if (serviceName.equals(Constant.BOM_PACKAGE)) {
            //选装包
            apiResult = saveVehiclePackage(esbVO);
        } else if (serviceName.equals(Constant.SAP_PRODUCTION_PLAN)) {
            //生产计划广播
            apiResult = productionPlan(esbVO);
        } else if (serviceName.equals(Constant.MES_TAIL)) {
            //车辆跟踪
            apiResult = saveVehicleTail(esbVO);

        } else if (serviceName.equals(Constant.MES_MF_INFO)) {
            //车身制造信息
            apiResult = saveVehicleMfInfo(esbVO);
        } else if (serviceName.equals(Constant.BOM_CONSTRAINT)) {
            //选装包约束
            apiResult = saveVehiclePackageConstraint(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_PROJECT)) {
            //CMS传递项目信息给PMS
            apiResult = saveProject(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_CATALOGCASE)) {
            //工时目录信息
            apiResult = saveManHourCatalogcase(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_DICTIONARY)) {
            //工时条目信息
            apiResult = saveManHourDictionary(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_MAN_HOUR_STEP)) {
            //工时步骤信息
            apiResult = saveManHourStep(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_BASE_MAN_HOUR_STEP)) {
            //工时基础步骤信息
            apiResult = saveBaseManHourStep(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_EPC_DICTIONARY)) {
            //EPC目录
            apiResult = saveEpcDictionary(esbVO);
        } else if (serviceName.equals(Constant.CMS_PMS_EPC_USAGE)) {
            //EPC零件用法
            apiResult = saveEpcUsage(esbVO);
        } /*else if (serviceName.equals(Constant.CMS_PMS_EPC_USAGE)) {
            //EPC零件用法
            apiResult = saveEpcUsage(esbVO);
        } */else if (serviceName.equals(Constant.CMS_PMS_EPC_REPLACE)) {
            //EPC替换关系
            apiResult = saveEpcReplace(esbVO);
        }else if (serviceName.equals(Constant.UPS_PMS_MAINTAIN_ORDER)){
            //维修工单
            apiResult = saveMaintainOrder(esbVO);
        } else {
            return ResponseUtil.fail(500, "没有对应的业务接口!");
        }
        return apiResult;
    }
    private ApiResult saveMaintainOrder(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("UPS工单系统发送维修工单");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //MaintainOrderVO maintainOrderVO= JSONObject.toJavaObject(JSON.parseObject(reqBody),MaintainOrderVO.class);
            //CmRepairOrderCreateReqVO cmRepairOrderReqVO =new CmRepairOrderCreateReqVO();
            //BeanUtils.copyProperties(maintainOrderVO,cmRepairOrderReqVO);
            CmRepairOrderCreateReqVO cmRepairOrderReqVO=JSONObject.toJavaObject(JSON.parseObject(reqBody),CmRepairOrderCreateReqVO.class);
            List<CmRepairOrderCreateReqVO> var1=new ArrayList<>();
            var1.add(cmRepairOrderReqVO);
            apiResult= cmRepairOrderDubboServiceImpl.saveOrUpdateBatch(var1);
            //调用内部接口
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveVehicleMfInfo(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        JSONObject jsonObject = JSON.parseObject(reqBody);
        ArrayList<VehicleMftInfoVO> vehicleMftInfoList = JSON.parseObject(String.valueOf(jsonObject.get("data")), new TypeReference<ArrayList<VehicleMftInfoVO>>() {
        });
        //调用业务接口传递list
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送车身制造信息给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.MES_MF_INFO);
        eaiLog.setRequestId(esbVO.getRequestId());
        try {
            ArrayList<HubVehicleMftInfoVO> hubVehicleMftInfoVO = new ArrayList<>();
            for (int i = 0; i < vehicleMftInfoList.size(); i++) {
                VehicleMftInfoVO vehicleMftInfo = vehicleMftInfoList.get(i);
                HubVehicleMftInfoVO hubVehicleMftInfo = new HubVehicleMftInfoVO();
                BeanUtils.copyProperties(vehicleMftInfo, hubVehicleMftInfo);
                hubVehicleMftInfoVO.add(hubVehicleMftInfo);
            }
            apiResult = hubProduceTraceServiceImpl.updateCarMftInfo(hubVehicleMftInfoVO);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //  SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }


    private ApiResult saveVehicleTail(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        JSONObject jsonObject = JSON.parseObject(reqBody);
        ArrayList<VehicleTrackVO> mesTailList = JSON.parseObject(String.valueOf(jsonObject.get("data")), new TypeReference<ArrayList<VehicleTrackVO>>() {
        });

        //调用业务接口传递list

        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI车辆跟踪信息给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.MES_TAIL);
        eaiLog.setRequestId(esbVO.getRequestId());
        try {
            //内部业务
            ArrayList<HubProduceTracePlanVO> hubProduceTracePlan = new ArrayList<>();
            for (int i = 0; i < mesTailList.size(); i++) {
                VehicleTrackVO vehicleTail = mesTailList.get(i);
                HubProduceTracePlanVO produceTracePlanVO = new HubProduceTracePlanVO();
                BeanUtils.copyProperties(vehicleTail, produceTracePlanVO);
                hubProduceTracePlan.add(produceTracePlanVO);
            }
            apiResult = hubProduceTraceServiceImpl.updateCarInfo(hubProduceTracePlan);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }

        try {
            List<VehicleOfflineReqVO> var1=new ArrayList<>();
            for (int i = 0; i < mesTailList.size(); i++) {
                VehicleTrackVO vehicleTail = mesTailList.get(i);
                VehicleOfflineReqVO vehicleOfflineReqVO=new VehicleOfflineReqVO();
                BeanUtils.copyProperties(vehicleTail, vehicleOfflineReqVO);
                var1.add(vehicleOfflineReqVO);
            }
            ApiResult upsResult = sapVehicleOfflineDubboServiceImpl.saveOrUpdateCarInfo(var1);
            if (upsResult.getCode()!=200){
                eaiLog.setDisposeStatus(3);
            }
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(3);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    //选装包
    private ApiResult saveVehiclePackage(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<PackageVO> packageList = JSON.parseObject(reqBody, new TypeReference<ArrayList<PackageVO>>() {
        });
        ArrayList<VehiclePackageReqVO> packageReList = new ArrayList<>();
        for (int i = 0; i < packageList.size(); i++) {
            PackageVO packageVO = packageList.get(i);
            VehiclePackageReqVO vehiclePackageReqVO = new VehiclePackageReqVO();
            BeanUtils.copyProperties(packageVO, vehiclePackageReqVO);
            packageReList.add(vehiclePackageReqVO);
        }
        String packageRe = JSONObject.toJSONString(packageReList);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(packageRe);
        eaiLog.setEsbEaiText(packageRe);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送选装包给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            apiResult = baseDataSaveServiceByBub.saveVehiclePackage(packageReList);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        //   Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
        }
        return apiResult;
    }

    //工程配置
    private ApiResult saveVehicleConfig(EsbVO esbVO) {
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        ArrayList<CatalogVO> catalogList = JSON.parseObject(reqBody, new TypeReference<ArrayList<CatalogVO>>() {
        });
        ArrayList<VehicleConfigReqVO> configList = new ArrayList<>();
        for (int i = 0; i < catalogList.size(); i++) {
            CatalogVO catalogVO = catalogList.get(i);
            VehicleConfigReqVO vehicleConfigReqVO = new VehicleConfigReqVO();
            BeanUtils.copyProperties(catalogVO, vehicleConfigReqVO);
            configList.add(vehicleConfigReqVO);
        }
        // System.out.println(catalogList);
        // apiResult= baseDataSaveService.saveVehicleConfig(configList);
        String congig = JSONObject.toJSONString(configList);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(congig);
        eaiLog.setEsbEaiText(congig);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送工程配置给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            apiResult = baseDataSaveServiceByBub.saveVehicleConfig(configList);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //  SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        return apiResult;
    }

    //车型
    private ApiResult saveVehicleSpectrum(EsbVO esbVO) {
        String reqBody = esbVO.getReqBody();
        ApiResult apiResult = null;
        ArrayList<ModelVO> modelList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ModelVO>>() {
        });
        ArrayList<VehicleSpectrumReqVO> spectrumlList = new ArrayList<>();
        for (int i = 0; i < modelList.size(); i++) {
            VehicleSpectrumReqVO vehicleSpectrumReqVO = new VehicleSpectrumReqVO();
            ModelVO modelVO = modelList.get(i);
            BeanUtils.copyProperties(modelVO, vehicleSpectrumReqVO);
            spectrumlList.add(vehicleSpectrumReqVO);
        }
        String spectrum = JSONObject.toJSONString(spectrumlList);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(spectrum);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送车辆型谱给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            apiResult = baseDataSaveServiceByBub.saveVehicleSpectrum(spectrumlList);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            for (int i = 0; i < spectrumlList.size(); i++) {
                com.yunxi.tech.pojo.VehicleSpectrumReqVO vehicleSpectrumReqVO=new com.yunxi.tech.pojo.VehicleSpectrumReqVO();
                VehicleSpectrumReqVO spectrumReqVO =  spectrumlList.get(i);
                BeanUtils.copyProperties(spectrumReqVO,vehicleSpectrumReqVO);
                ApiResult upsResult = iamVehicleSpectrumDubboServiceImpl.saveSpectrum(vehicleSpectrumReqVO);
                if (upsResult.getCode()!=200){
                    eaiLog.setDisposeStatus(3);
                }
            }
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            eaiLog.setDisposeStatus(3);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult productionPlan(EsbVO esbVO) {
        ApiResult apiResult = null;
        EaiLog eaiLog = new EaiLog();
        String reqBody = esbVO.getReqBody();
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("ESB发送生产计划广播给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setServiceName(esbVO.getServiceName());
        JSONObject jsonObject = JSON.parseObject(reqBody);
        ArrayList<ProductionPlanVO> productionPlan = JSON.parseObject(String.valueOf(jsonObject.get("orderlist")), new TypeReference<ArrayList<ProductionPlanVO>>() {
        });
        try {
            //内部业务

            List<HubProducePlanVO> list = new ArrayList();
            for (int i = 0; i < productionPlan.size(); i++) {
                HubProducePlanVO hubProducePlanVO = new HubProducePlanVO();
                ProductionPlanVO productionPlanVO = productionPlan.get(i);
                BeanUtils.copyProperties(productionPlanVO, hubProducePlanVO);
                list.add(hubProducePlanVO);
            }
            producePlanBroadcastDubboServiceImpl.save(list);
           /* if (apiResult.getCode()==-1){
                eaiLog.setDisposeStatus(2);
            }*/
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
        }
        //  Message message = new Message(rocketConfig.getEaiLogTopic(), "eaiLog",(JSONObject.toJSONString(eaiLog)).getBytes() );
        try {
            //    SendResult send = eaiLogProducer.getProducer().send(message);
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    /**
     * 选装包约束
     *
     * @param esbVO
     * @return
     */
    private ApiResult saveVehiclePackageConstraint(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        // ArrayList<PackageConstraintBomVO> packageConstraintList = JSON.parseObject(reqBody, new TypeReference<ArrayList<PackageConstraintBomVO>>() {});
        PackageConstraintVO packageConstraintVO = JSON.parseObject(esbVO.getReqBody(), PackageConstraintVO.class);
        String packageConstraint = JSONObject.toJSONString(packageConstraintVO);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setEsbEaiContent(packageConstraint);
        eaiLog.setEsbEaiText(packageConstraint);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("EAI系统发送约束规则给PMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            List<PackageConstraintVO> dataList = new ArrayList<>();
           /* PackageConstraintVO packageConstraintVO = new PackageConstraintVO();
            BeanUtils.copyProperties(packageConstraintBomVO, packageConstraintVO);*/
            dataList.add(packageConstraintVO);
            apiResult = constraintConfigDubboServiceImpl.receiveBomData(dataList);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            sendConstraintPMS2APP(reqBody);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }


    /**
     * @param reqBody
     * @return
     */
    public ApiResult sendConstraintPMS2APP(String reqBody) {
        ApiResult apiResult = null;
        EaiLog eaiLog = new EaiLog();
        eaiLog.setPmsEaiContent(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送约束规则给APP系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_BOM_CONSTRAINT);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try {
            Map map = HttpUtil.sendPost(reqBody, Constant.PMS_BOM_CONSTRAINT, Constant.PMS, APP_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setEsbEaiText(JSONObject.toJSONString(map));
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag = String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")) {//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult = ResponseUtil.fail();
            } else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (Integer.parseInt(String.valueOf(jsonResponse.get("code"))) != 200) {
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult = ResponseUtil.fail();
                } else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        } catch (Exception e) {
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(), e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult = ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
        }
        return apiResult;
    }

    private ApiResult saveEpcReplace(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
      /*  ArrayList<EpcReplaceVO> epcReplaceList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcReplaceVO>>() {
        });*/
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC替换关系给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            ArrayList<PrEpcPartReplacementCreateReqVO> var1 = JSON.parseObject(reqBody, new TypeReference<ArrayList<PrEpcPartReplacementCreateReqVO>>() {
            });
            apiResult =eaiEpcPartReplacementDubboServiceImpl.saveOrUpdate(var1);
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveEpcUsage(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        /*ArrayList<EpcDictionaryVO> epcDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcDictionaryVO>>() {
        });*/
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            ArrayList<PrEpcPartCreateReqVO> var1 = JSON.parseObject(reqBody, new TypeReference<ArrayList<PrEpcPartCreateReqVO>>() {
            });
            apiResult =eaiEpcPartDubboServiceImpl.saveOrUpdate(var1);
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveEpcDictionary(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
       /* ArrayList<EpcDictionaryVO> epcDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<EpcDictionaryVO>>() {
        });*/
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递EPC目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            ArrayList<PrEpcCatalogCreateReqVO> var1 = JSON.parseObject(reqBody, new TypeReference<ArrayList<PrEpcCatalogCreateReqVO>>() {
            });
            apiResult=eaiEpcCatalogDubboServiceImpl.saveOrUpdate(var1);
            if (apiResult.getCode() == -1) {
                eaiLog.setDisposeStatus(2);
            }
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveBaseManHourStep(EsbVO esbVO) {
/*        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<BaseManHourStepVO> baseManHourStepList = JSON.parseObject(reqBody, new TypeReference<ArrayList<BaseManHourStepVO>>() {
        });
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时基础步骤信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmLabourBasicStepReqVO> var1=new ArrayList<>();
            for (int i = 0; i < baseManHourStepList.size(); i++) {
                CmLabourBasicStepReqVO cmLabourBasicStepReqVO=dozerMapper.map(baseManHourStepList.get(i),CmLabourBasicStepReqVO.class);
                var1.add(cmLabourBasicStepReqVO);
            }
            cmLabourBasicStepDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            apiResult=ResponseUtil.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;*/
        ApiResult apiResult = null;
        try {
            SendResult result=cmsBaseManHourStepProducer.sendMessage(JSONObject.toJSONString(esbVO));
            apiResult=ResponseUtil.ok(JSONObject.toJSONString(result));
        }catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    private ApiResult saveManHourStep(EsbVO esbVO) {
      /*  ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<ManHourStepVO> manHourStepList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourStepVO>>() {
        });
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时步骤信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmLabourStepReqVO> var1=new ArrayList<>();
            for (int i = 0; i < manHourStepList.size(); i++) {
                CmLabourStepReqVO cmLabourStepReqVO=dozerMapper.map(manHourStepList.get(i), CmLabourStepReqVO.class);
                var1.add(cmLabourStepReqVO);
            }
            cmLabourStepDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            apiResult=ResponseUtil.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;*/
        ApiResult apiResult = null;
        try {
            SendResult result =cmsHourStepProducer.sendMessage(JSONObject.toJSONString(esbVO));
            apiResult=ResponseUtil.ok(JSONObject.toJSONString(result));
        }catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    private ApiResult saveManHourDictionary(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ArrayList<ManHourDictionaryVO> manHourDictionaryList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourDictionaryVO>>() {
        });
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时条目信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmLabourEntryReqVO> var1=new ArrayList<>();
            for (int i = 0; i < manHourDictionaryList.size(); i++) {
                CmLabourEntryReqVO cmLabourEntryReqVO = dozerMapper.map(manHourDictionaryList.get(i), CmLabourEntryReqVO.class);
                var1.add(cmLabourEntryReqVO);
            }
            cmLabourEntryDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            apiResult=ResponseUtil.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveManHourCatalogcase(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        // ManHourCatalogcaseVO manHourCatalogcaseVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), ManHourCatalogcaseVO.class);
        ArrayList<ManHourCatalogcaseVO> manHourCatalogcaseList = JSON.parseObject(reqBody, new TypeReference<ArrayList<ManHourCatalogcaseVO>>() {
        });
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递工时目录信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmLabourDirectoryReqVO> var1 = new ArrayList<>();
            for (int i = 0; i < manHourCatalogcaseList.size(); i++) {
                CmLabourDirectoryReqVO cmLabourDirectoryReqVO = dozerMapper.map(manHourCatalogcaseList.get(i), CmLabourDirectoryReqVO.class);
                var1.add(cmLabourDirectoryReqVO);
            }
            cmLabourDirectoryDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            apiResult=ResponseUtil.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }

    private ApiResult saveProject(EsbVO esbVO) {
        ApiResult apiResult = null;
        String reqBody = esbVO.getReqBody();
        ProjectVO projectVO = JSONObject.toJavaObject(JSON.parseObject(reqBody), ProjectVO.class);
        EaiLog eaiLog = new EaiLog();
        eaiLog.setRequestId(esbVO.getRequestId());
        eaiLog.setEsbEaiContent(reqBody);
        eaiLog.setEsbEaiText(reqBody);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("CMS传递项目信息给PMS");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(esbVO.getServiceName());
        try {
            //调用内部接口
            List<CmLabourProjectReqVO> var1 = new ArrayList<>();
            CmLabourProjectReqVO cmLabourProjectReqVO = dozerMapper.map(projectVO, CmLabourProjectReqVO.class);
            var1.add(cmLabourProjectReqVO);
            cmLabourProjectDubboServiceImpl.saveOrUpdateBatch(var1);
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setPmsResultContent(JSONObject.toJSONString(apiResult));
            apiResult=ResponseUtil.ok();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            apiResult = ResponseUtil.fail();
            eaiLog.setDisposeStatus(2);
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
        return apiResult;
    }
}
